package fonkin.com.wifisignapp.acticity.aboutMe;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.adapter.news.MessageAdapter;
import fonkin.com.wifisignapp.adapter.news.ScheduleAdapter;
import fonkin.com.wifisignapp.bean.schedule.Schedule;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.utils.PrintLog;
import fonkin.com.wifisignapp.utils.ReturnVo;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Message extends AppCompatActivity {

    ListView messageListView;
    MessageAdapter messageAdapter;
    ImageView noMessageData;
    String userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        messageListView = (ListView)findViewById(R.id.message_listview);
        noMessageData = (ImageView) findViewById(R.id.type_no_message);
        SharedPreferences preferences =  PreferenceManager.getDefaultSharedPreferences(Message.this);
        userId = preferences.getString("user_id","");
        BaseRetrofit.GetBaseRetrofit().getMessage(userId).enqueue(new Callback<ReturnVo<List<Schedule>>>() {
            @Override
            public void onResponse(Call<ReturnVo<List<Schedule>>> call, Response<ReturnVo<List<Schedule>>> response) {
                if (response.isSuccessful()){
                    PrintLog.printApi(response,call);
                    messageAdapter = new MessageAdapter(Message.this, new ArrayList<Schedule>());
                    messageListView.setAdapter(messageAdapter);
                    if (response.body().getCode().equals(1)){
                        List<Schedule> data = response.body().getData();
                        if (!data.isEmpty()){
                            messageAdapter.addAll(data);
                        }else {
                            noMessageData.setVisibility(View.VISIBLE);
                            Picasso
                                    .with(Message.this)
                                    .load(R.drawable.none_data)
                                    .resize(500,400)
                                    .into(noMessageData);
                        }

                    }
                }
            }

            @Override
            public void onFailure(Call<ReturnVo<List<Schedule>>> call, Throwable t) {

            }
        });

    }
}
